package com.atguigu.nio;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel03 {
    public static void main(String[] args) throws IOException {
        File file = new File("d:\\file01.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel inputStreamChannel = fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("d:\\file02.txt");
        FileChannel outputStreamChannel = fileOutputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(10);
        System.out.println("文件大小：" + file.length());
        while (true) {
            int read = inputStreamChannel.read(buffer);
            if (read == -1) {
                break;
            }
            buffer.flip();
            outputStreamChannel.write(buffer);
            buffer.clear();
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}
